package org.bendev.ildaplayer;

import android.content.Context;
import android.util.AttributeSet;
import android.view.TextureView;

public class VideoView extends TextureView {
	static final String TAG = "VideoView";

	int mVideoWidth = 0, mVideoHeight = 0;

	public VideoView( Context context ) {
		super( context );
	}

	public VideoView( Context context, AttributeSet attrs ) {
		super( context, attrs );
	}

	@Override
	protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec ) {
		int width = getDefaultSize( mVideoWidth, widthMeasureSpec );
		int height = getDefaultSize( mVideoHeight, heightMeasureSpec );

		if( mVideoWidth > 0 && mVideoHeight > 0 ){
			if( mVideoWidth * height > width * mVideoHeight ) height = width * mVideoHeight / mVideoWidth;
			else if( mVideoWidth * height < width * mVideoHeight ) width = height * mVideoWidth / mVideoHeight;
		}
		setMeasuredDimension( width, height );
	}

	public void setVideoSize( int width, int height ) {
		if( mVideoWidth==width && mVideoHeight==height )
			return;
		
		mVideoWidth = width;
		mVideoHeight = height;
		requestLayout();
		invalidate();
	}
}
